$(document).ready(function() {
		
	$('#hiredate').datepicker({
		maxDate: "+0D"
	});
	
	$('#hiredate-add').datepicker({
		maxDate: "+0D"
	});
	
	
	
	$('#updateEmployee').click(function(){
		updateEmpDetails();
	});
	
	$('#addEmployee-btn').click(function(){
		$("#addEmployee-box").dialog("open");
	});
	
	
	$("#addEmployee-box").dialog({
	    resizable: false,
	    modal: true,
	    minHeight: 400,
		width: 700,
	    buttons: {
	    	"Save" : function () {
	    		addEmployee();
	        }
	        ,"Cancel" : function () {
	            $(this).dialog("close");
	        }
	    }
	});
	
	$("#addEmployee-box").dialog("close");
		
});

function getEmpDetails(empId){
	var request = {
			employeeId : empId
		};
	
	$("#loading-message").text("Loading Employee Information. Please wait.");
	$("#loading-box").dialog("open");
	$.post('index.php?c=admin&m=getEmployee', request , function(result,status) {
		
		//set values
		$("#employeeId").val(result.employeeId);
		$("#firstname").val(result.firstname);
		$("#lastname").val(result.lastname);
		$("#contact").val(result.contact);
		dateHired = new Date(result.hiredate);
		$("#hiredate").datepicker("setDate", new Date(dateHired.getFullYear(), dateHired.getMonth(), dateHired.getDate()));
		$("#address").val(result.address);
		$("#branch").val(result.branch);
		$("#position").val(result.position);
		$("#empStatus").val(result.status);
		
		$("#updateEmployee").show();		
		$("#loading-box").dialog("close");
	},"json").fail(function() { $("#loading-box").dialog("close"); });
}

function updateEmpDetails(){
	
	//validation here
	errors = validateFields("");
	
	if(errors.length > 0){
		$("#loading-box").dialog("close");
		
		errorMsg="<span class='bold red'>Kindly check the following:</span><br />"
		
		for (var i = 0; i < errors.length; i++) {
			errorMsg = errorMsg + "<br />" + errors[i];
		}
		
		$("#error-message").html(errorMsg);
		$("#error-box").dialog("open");
	} else {
		
		empId = $("#employeeId").val();
		fname = $("#firstname").val();
		lname = $("#lastname").val();
		con = $("#contact").val();
		hrdate = $("#hiredate").val();
		add= $("#address").val();
		br = $("#branch").val();
		pos = $("#position").val();
		empStat = $("#empStatus").val();
		
		var request = {
				employeeId : empId,
				firstname : fname, 
				lastname : lname, 
				contact : con,
				hiredate : hrdate, 
				address : add,
				branch : br, 
				position : pos, 
				status : empStat 
			};
		
		$("#loading-message").text("Updating Employee Information. Please wait.");
		$("#loading-box").dialog("open");
		
		$.post('index.php?c=admin&m=updateEmployee', request , function(result,status) {
			
			$("#loading-box").dialog("close");
			
			if(result.rowsAffected > 0){
				$("#alert-message").text("Update Successful!");
			} else {
				$("#alert-message").text("Nothing has been changed!");
			}
			$("#alert-box").dialog("open");
			
		},"json").fail(function() { $("#loading-box").dialog("close"); });
	}
	

	
}

function addEmployee(){
	errors = validateFields("-add");
	
	if(errors.length > 0){
		$("#loading-box").dialog("close");
		
		errorMsg="<span class='bold red'>Kindly check the following:</span><br />";
		
		for (var i = 0; i < errors.length; i++) {
			errorMsg = errorMsg + "<br />" + errors[i];
		}
		
		$("#error-message").html(errorMsg);
		$("#error-box").dialog("open");
	} else {
		
		fname = $("#firstname-add").val();
		lname = $("#lastname-add").val();
		con = $("#contact-add").val();
		hrdate = $("#hiredate-add").val();
		add= $("#address-add").val();
		br = $("#branch-add").val();
		pos = $("#position-add").val();
		
		var request = {
				firstname : fname, 
				lastname : lname, 
				contact : con,
				hiredate : hrdate, 
				address : add,
				branch : br, 
				position : pos, 
			};
		
		$("#loading-message").text("Saving New Employee Information. Please wait.");
		$("#loading-box").dialog("open");
		
		$.post('index.php?c=admin&m=saveEmployee', request , function(result,status) {
			
			$("#loading-box").dialog("close");
			
			if(result.rowsAffected > 0){
				$("#addEmployee-box").dialog("close");
				$("#alert-message").html("<span class='bold'>New Employee Added!</span> <br /> <span class='bold red'>LASTNAME</span> provided is the default password.");
				$("#alert-box").dialog({
				    buttons: {
				        "OK" : function () {
				        	window.location.href = "index.php?c=admin&m=adminEmployee";
				        }
				    }
				});
			} else {
				$("#alert-message").text("Nothing added!");
			}
			$("#alert-box").dialog("open");
			
		},"json").fail(function() { $("#loading-box").dialog("close"); });
	}
}

function validateFields(actionInd){
	
	error = new Array();
	
	fname = $("#firstname" + actionInd).val().trim();
	lname = $("#lastname" + actionInd).val().trim();
	con = $("#contact" + actionInd).val().trim();
	hrdate = $("#hiredate" + actionInd).val().trim();
	add= $("#address" + actionInd).val().trim();
	
	//firstname
	if(fname == '' || isNumber(fname) == true){
		error.push("- Firstname");
	}
	
	//lastname
	if(lname == '' || isNumber(lname) == true){
		error.push("- Lastname");
	}
	
	//contact
	if(con == ''){
		error.push("- Contact Number");
	}
	
	//hiredate
	if(hrdate == ''){
		error.push("- Hiredate");
	}
	
	//address
	if(add == ''){
		error.push("- Address");
	}
	
	return error;
}

function isNumber(n) {
	return !isNaN(parseFloat(n)) && isFinite(n);
}

//MYACCOUNT
function updateAccount(){
	
	var newPassword = $("#newPass").val();
	var retypePassword = $("#retypePass").val();
	

	if(newPassword != retypePassword){
		$("#error-message").text("New password and Re-type password doesn't match.");
		$("#error-box").dialog("open");
	} else {
		//updatePassword
		var url = getUrl('admin', 'updateAccount');
		$("#loading-message").text("Updating account password.");
		$("#loading-box").dialog("open");
		
		var request = {
				password: newPassword
		}
		
		$.post(url, request , function(result,status) {
			
			$("#loading-box").dialog("close");
			
			if(result.rowsAffected > 0){
				$("#alert-message").html("Account password update SUCCESSFUL!");
				$("#alert-box").dialog({
				    buttons: {
				        "OK" : function () {
				        	var urlBack = getUrl('admin', 'myAccount');
				        	window.location.href = urlBack;
				        }
				    }
				});
			} else {
				$("#alert-message").text("Nothing has been changed!");
			}
			
			$("#alert-box").dialog("open");
			
		},"json").fail(function() {$("#loading-box").dialog("close"); });
	}
	
}
